"use strict";
cc._RF.push(module, '81957Ty3I5JDZZE1he0lLfA', 'GamePlayerController');
// scripts/pfb/GamePlayerController.ts

"use strict";
// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var NickNameLabel_1 = require("../util/NickNameLabel");
var Tools_1 = require("../util/Tools");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var GamePlayerController = /** @class */ (function (_super) {
    __extends(GamePlayerController, _super);
    function GamePlayerController() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.avatar = null; //头像
        _this.nameLabel = null; //用户昵称
        _this.playerTypeImage = null; //玩家类型图片节点
        _this.disconnectNode = null; //断线状态节点
        _this.leaveNode = null; //退出状态节点
        _this.deathNode = null; //死亡状态节点
        // 用户ID，用于标识这个头像预制体对应的用户
        _this.userId = "";
        return _this;
        // update (dt) {}
    }
    // onLoad () {}
    GamePlayerController.prototype.start = function () {
    };
    GamePlayerController.prototype.setData = function (user) {
        var _this = this;
        this.scheduleOnce(function () {
            if (user == null) {
                _this.avatar.active = false;
                _this.userId = "";
                // 清空时也隐藏所有状态节点
                _this.initializeStatusNodes();
            }
            else {
                // 保存用户ID
                _this.userId = user.userId;
                // 开局时隐藏所有状态节点
                _this.initializeStatusNodes();
                // 检查必要的组件是否存在
                if (!_this.avatar) {
                    console.error("avatar 节点未设置！");
                    return;
                }
                if (!_this.nameLabel) {
                    console.error("nameLabel 节点未设置！");
                    return;
                }
                if (!_this.playerTypeImage) {
                    console.error("playerTypeImage 节点未设置！");
                    return;
                }
                // 设置头像
                if (user.avatar && user.avatar.trim() !== '') {
                    // 确保头像节点可见
                    _this.avatar.active = true;
                    _this.avatar.opacity = 255;
                    // 加载头像
                    Tools_1.Tools.setNodeSpriteFrameUrl(_this.avatar, user.avatar);
                    // 延迟检查头像是否设置成功
                    _this.scheduleOnce(function () {
                        var sprite = _this.avatar.getComponent(cc.Sprite);
                    }, 1.0);
                }
                else {
                    console.warn("头像URL为空，隐藏头像节点");
                    _this.avatar.active = false;
                }
                // 设置昵称
                if (user.nickName && user.nickName.trim() !== '') {
                    var nicknameLabel = _this.nameLabel.getComponent(NickNameLabel_1.default);
                    if (nicknameLabel) {
                        nicknameLabel.string = user.nickName;
                    }
                    else {
                        // 如果没有NickNameLabel组件，直接设置Label
                        _this.nameLabel.string = user.nickName;
                    }
                }
                else {
                    console.warn("昵称为空");
                    if (_this.nameLabel) {
                        _this.nameLabel.string = "未知用户";
                    }
                }
            }
        }, 0.1);
    };
    /**
     * 设置玩家类型图像
     * @param playerType 玩家类型 (red, yellow, green, pink)
     */
    GamePlayerController.prototype.setPlayerTypeImage = function (playerType) {
        var _this = this;
        if (!this.playerTypeImage) {
            console.error("playerTypeImage 节点未设置！");
            return;
        }
        if (!playerType || playerType.trim() === '') {
            console.warn("玩家类型为空，隐藏类型图片");
            this.playerTypeImage.active = false;
            return;
        }
        // 显示类型图片节点
        this.playerTypeImage.active = true;
        // 根据playerType加载对应的图片资源
        var imagePath = "player_res/" + playerType;
        cc.resources.load(imagePath, cc.SpriteFrame, function (err, spriteFrame) {
            if (err) {
                console.error("\u52A0\u8F7D\u73A9\u5BB6\u7C7B\u578B\u56FE\u7247\u5931\u8D25: " + imagePath, err);
                _this.playerTypeImage.active = false;
                return;
            }
            var sprite = _this.playerTypeImage.getComponent(cc.Sprite);
            if (sprite) {
                sprite.spriteFrame = spriteFrame;
            }
            else {
                console.error("playerTypeImage 节点缺少 Sprite 组件");
            }
        });
    };
    /**
     * 只设置玩家类型图像，不生成动物预制体
     * @param playerType 玩家类型
     */
    GamePlayerController.prototype.setPlayerTypeImageOnly = function (playerType) {
        this.setPlayerTypeImage(playerType);
    };
    /**
     * 获取用户ID
     */
    GamePlayerController.prototype.getUserId = function () {
        return this.userId;
    };
    /**
     * 显示玩家状态节点
     * @param eventType 事件类型 (7-死亡, 8-断线, 9-退出, 10-重连)
     */
    GamePlayerController.prototype.showPlayerStatus = function (eventType) {
        switch (eventType) {
            case 7: // 死亡
                this.showDeathStatus();
                break;
            case 8: // 断线
                this.showDisconnectStatus();
                break;
            case 9: // 退出
                this.showLeaveStatus();
                break;
            case 10: // 重连 - 隐藏断线状态
                this.hideDisconnectStatus();
                break;
            default:
                console.warn("\u672A\u77E5\u7684\u73A9\u5BB6\u72B6\u6001\u4E8B\u4EF6\u7C7B\u578B: " + eventType);
        }
    };
    /**
     * 根据OnlineStateEvent显示状态（用于断线重连）
     * @param onlineStateEvent OnlineStateEvent值 (7-死亡, 8-离线, 9-离开, 10-在线/重连)
     */
    GamePlayerController.prototype.showStatusByOnlineStateEvent = function (onlineStateEvent) {
        // 先隐藏所有状态节点
        this.hideAllStatusNodes();
        switch (onlineStateEvent) {
            case 7: // 死亡
                this.showDeathStatus();
                break;
            case 8: // 离线
                this.showDisconnectStatus();
                break;
            case 9: // 离开
                this.showLeaveStatus();
                break;
            case 10: // 在线/重连
                // 不显示任何状态节点
                break;
            default:
                console.warn("\u672A\u77E5\u7684OnlineStateEvent: " + onlineStateEvent);
        }
    };
    /**
     * 显示死亡状态
     */
    GamePlayerController.prototype.showDeathStatus = function () {
        this.hideAllStatusNodes();
        if (this.deathNode) {
            this.deathNode.active = true;
        }
        else {
            console.error("deathNode 节点未设置！请在编辑器中设置deathNode");
        }
    };
    /**
     * 显示断线状态
     */
    GamePlayerController.prototype.showDisconnectStatus = function () {
        // 根据优先级，如果已经显示退出状态，则不显示断线状态
        if (this.leaveNode && this.leaveNode.active) {
            return;
        }
        this.hideOtherStatusNodes('disconnect');
        if (this.disconnectNode) {
            this.disconnectNode.active = true;
        }
        else {
            console.error("disconnectNode 节点未设置！请在编辑器中设置disconnectNode");
        }
    };
    /**
     * 显示退出状态
     */
    GamePlayerController.prototype.showLeaveStatus = function () {
        this.hideAllStatusNodes();
        if (this.leaveNode) {
            this.leaveNode.active = true;
        }
        else {
            console.error("leaveNode 节点未设置！请在编辑器中设置leaveNode");
        }
    };
    /**
     * 隐藏断线状态
     */
    GamePlayerController.prototype.hideDisconnectStatus = function () {
        if (this.disconnectNode) {
            this.disconnectNode.active = false;
        }
    };
    /**
     * 隐藏所有状态节点
     */
    GamePlayerController.prototype.hideAllStatusNodes = function () {
        if (this.disconnectNode)
            this.disconnectNode.active = false;
        if (this.leaveNode)
            this.leaveNode.active = false;
        if (this.deathNode)
            this.deathNode.active = false;
    };
    /**
     * 隐藏除指定类型外的其他状态节点
     * @param keepType 保持显示的状态类型
     */
    GamePlayerController.prototype.hideOtherStatusNodes = function (keepType) {
        if (keepType !== 'disconnect' && this.disconnectNode) {
            this.disconnectNode.active = false;
        }
        if (keepType !== 'leave' && this.leaveNode) {
            this.leaveNode.active = false;
        }
        if (keepType !== 'death' && this.deathNode) {
            this.deathNode.active = false;
        }
    };
    /**
     * 初始化状态节点（隐藏所有状态节点）
     */
    GamePlayerController.prototype.initializeStatusNodes = function () {
        this.hideAllStatusNodes();
    };
    __decorate([
        property(cc.Node)
    ], GamePlayerController.prototype, "avatar", void 0);
    __decorate([
        property(cc.Label)
    ], GamePlayerController.prototype, "nameLabel", void 0);
    __decorate([
        property(cc.Node)
    ], GamePlayerController.prototype, "playerTypeImage", void 0);
    __decorate([
        property(cc.Node)
    ], GamePlayerController.prototype, "disconnectNode", void 0);
    __decorate([
        property(cc.Node)
    ], GamePlayerController.prototype, "leaveNode", void 0);
    __decorate([
        property(cc.Node)
    ], GamePlayerController.prototype, "deathNode", void 0);
    GamePlayerController = __decorate([
        ccclass
    ], GamePlayerController);
    return GamePlayerController;
}(cc.Component));
exports.default = GamePlayerController;

cc._RF.pop();